﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using tf2hudmaker.Properties;

namespace tf2hudmaker
{
    // Generic class that represent all the Data of a specific .res file
    class Data
    {
        // CONSTRUCTORS
        public Data()
        {

        }

        // PROPERTIES
        protected string RessourceFileName;
        protected string RessourceFilePath;
        public List<FieldInfo> FieldList;

        // METHODS
        // Set all the fields of the .res file at the defaults TF2 values
        public virtual void SetDefaultValues()
        {
            this.FieldList.Clear();
        }

        // Make the content of the final .res file
        private string BuildFinalContent()
        {
            // Get the sample file content
            string pathoftheexe = ExportManager.pathoftheexe;
            string samplefilepath =  pathoftheexe + "\\SampleFiles\\" + RessourceFilePath + "sample_" + RessourceFileName;
            string samplecontent = File.ReadAllText(samplefilepath);
          


            // Update the sample content by replacing each fields
            for (int i = 0; i < FieldList.Count; i++)
            {
                FieldInfo fieldinfo = FieldList[i];
                samplecontent = samplecontent.Replace(fieldinfo.ReplaceString, fieldinfo.GetFinalString());
            }

            return samplecontent;
        }

        // Create the final .res file
        public bool ExportFile()
        {
            string tf2directory = Settings.Default._tf2path;
            string finalpath = tf2directory + "\\tf\\custom\\tf2hudmakerexport\\" + RessourceFilePath + RessourceFileName;

            try
            {
                using (StreamWriter stream = File.CreateText(finalpath))
                {
                    stream.Write(this.BuildFinalContent());
                    stream.Close();

                    Console.WriteLine("Exported : " + finalpath);
                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Fail to export : " + finalpath);
                Console.WriteLine("Reason: " + e.Message);
                return false;
            }
        }
    }
}
